package com.pricecmp.model;

import java.util.Date;

public class Good implements Comparable<Good>{
	private Long goodId;
	private String name;
	private String category;
	private String price;
	private String url;
	private String imgUrl;
	private Date fetchTime;
	private String source;

	public Good() {
	}

	public Good(Long goodId, String name, String category, String price,
			String url, String imgUrl, Date fetchTime, String source) {
		super();
		this.goodId = goodId;
		this.name = name;
		this.category = category;
		this.price = price;
		this.url = url;
		this.imgUrl = imgUrl;
		this.fetchTime = fetchTime;
		this.source = source;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getImgUrl() {
		return imgUrl;
	}

	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}

	public Date getFetchTime() {
		return fetchTime;
	}

	public void setFetchTime(Date fetchTime) {
		this.fetchTime = fetchTime;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public Long getGoodId() {
		return goodId;
	}

	public void setGoodId(Long goodId) {
		this.goodId = goodId;
	}



	@Override
	public int compareTo(Good o) {
		// TODO Auto-generated method stub
		double thisDouble=0;
		double otherDouble=0;
		String thisPrice=this.getPrice();
		String otherPrice=o.getPrice();
		int thisIndex=0;
		int otherIndex=0;
		
		if((thisIndex=thisPrice.indexOf('-'))>0)
			thisPrice=thisPrice.substring(0, thisIndex);
		
		if((otherIndex=otherPrice.indexOf('-'))>0)
			otherPrice=otherPrice.substring(0,otherIndex);
		otherPrice=otherPrice.replaceAll(",", "");
		thisPrice=thisPrice.replaceAll(",", "");
		if(thisPrice.startsWith("￥"))
		{			
			thisPrice=thisPrice.substring(1);
			this.setPrice(thisPrice);
		}
		else if(thisPrice.startsWith("$"))
		{
			thisPrice=thisPrice.substring(1);
			thisDouble=6.2*Double.parseDouble(thisPrice);
			this.setPrice(thisDouble+"");
		}
		if(otherPrice.startsWith("￥"))
		{
			otherPrice=otherPrice.substring(1);
			o.setPrice(otherPrice);
		}
		else if(otherPrice.startsWith("$"))
		{
			otherPrice=otherPrice.substring(1);
			otherDouble=6.2*Double.parseDouble(otherPrice);
			o.setPrice(otherDouble+"");
		}
		return (int)(Double.parseDouble(this.getPrice())-Double.parseDouble(o.getPrice()));
	}

}
